// pages/settings/inbound/enter/enter.js
const util = require('../../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: "2016-09-01",
    name: "",
    id: "",
    price: "", //单价
    unit: "",
    total: "",
    bag: "0",
    box: "",
    originalPrice: "" //默认价格
  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
    console.log(e)
  },

  showTopTips: function() {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 1500);
  },

  formSubmit: function(e) {
    var that = this

    if (!e.detail.value.box || (e.detail.value.box == 0 && e.detail.value.bag == 0) || !e.detail.value.bag) {
      this.showTopTips();
      return; //验证提交是否为空
    }

    if ((this.data.price / this.data.unit) != this.data.originalPrice) {
      wx.showModal({
        title: '价格变动',
        content: "单价变动了！！！\n入库单价与商品单价不一致，仍然要以这个价格继续入库吗？",
        confirmText: "确认",
        cancelText: "取消", //验证单价是否变动
        confirmColor: '#ffd948',
        success: function(res) {

          if (res.cancel) {
            return;
          }
          if (res.confirm) {
            that.requestInbound(e) 
          }
        }
      })
    } 
    else {
      that.requestInbound(e) 
    }
  },

  uniqueNotice: function(e) {
    var that = this
    wx.showModal({
      title: '未找到商品',
      content: '仓库里还没有这件商品，仍然要添加吗？',
      confirmText: "确认",
      cancelText: "取消", //仓库无此商品 则提示
      confirmColor: '#ffd948',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: 'http://192.168.3.2/cloudwarehouse/settings/inbound/enter/enterNew.php',
            data: {
              name: that.data.name,
              id: that.data.id,
              box: e.detail.value.box,
              bag: e.detail.value.bag,
              date: that.data.date,
              subdate: util.getNowDate(),
              openid: util.getOpenid(),
              price: that.data.price,
              unit: that.data.unit
            },
            success: function(res) {
              console.log(res.data)
              that.showSuccess(e)
            }
          })
        }
      }
    })
  },



  requestInbound: function(e) {
    var that = this
    wx.request({
      url: 'http://192.168.3.2/cloudwarehouse/settings/inbound/enter/enter.php',
      data: {
        name: that.data.name,
        id: that.data.id,
        box: e.detail.value.box,
        bag: e.detail.value.bag,
        date: that.data.date,
        subdate: util.getNowDate(),
        openid: util.getOpenid(),
        price: that.data.price,
        unit: that.data.unit
      },
      success: function(res) {
        console.log(res.data)
        if(res.data == -1)
          that.uniqueNotice(e)
        else
          that.showSuccess()
      }
    })
  },




  showSuccess: function(e) {
    var key = 1
    wx.navigateTo({
      url: '../entersuccessfully/entersuccessfully',
    })
  },

  endBox: function(e) {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      box: Number(e.detail.value),
    })
    this.setData({
      total: Number((this.data.box * this.data.price + this.data.bag / this.data.unit * this.data.price).toFixed(2))
    })
    wx.hideLoading()
  },

  endBag: function(e) {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      bag: Number(e.detail.value)
    })
    this.setData({
      total: Number((this.data.box * this.data.price + this.data.bag / this.data.unit * this.data.price).toFixed(2))
    })
    wx.hideLoading()
  },

  endTotal: function(e) {
    this.setData({
      price: Number(e.detail.value)
    })
    this.setData({

      total: Number((this.data.box * this.data.price + this.data.bag / this.data.unit * this.data.price).toFixed(2))
    })
  },


  /**
   * 
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      date: util.getNowFormatDate()
    })
    console.log(util.getNowDate())
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    wx.getStorage({
      key: 'selectedCommodity',
      success: function(res) {
        that.setData({
          name: res.data
        })
      },
    })
    wx.getStorage({
      key: 'selectedId',
      success: function(res) {
        that.setData({
          id: res.data
        })
      },
    })
    wx.getStorage({
      key: 'selectedPrice',
      success: function(res) {
        that.setData({
          originalPrice: Number(res.data)
        })
        var p = res.data
        wx.getStorage({
          key: 'selectedUnit',
          success: function(res) {
            that.setData({
              unit: res.data,
              price: Number(p * res.data).toFixed(2)
            })
            that.setData({

              total: Number(that.data.box * that.data.price + that.data.bag / that.data.unit * that.data.price)
            })
          },
        })

      },
    })

  },

})